﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Objects
{
    public class Bullet : BaseObject
    {
        
        Vector2 Direction;
        //RoyalRumble.MainGame mainGame = new RoyalRumble.MainGame();
        public Bullet(Tank bulletOwner)
        {
            //Each bullet has a owner, that's the tank. The tank has an angle that gives what angle the turret is facing. 
            Owner = bulletOwner;
            RenderColor = Color.White;
            Position = Owner.Position;
            Orientation = bulletOwner.TurretOrientation; //Which way is it facing
            Direction = new Vector2((float)Math.Sin(Orientation), -(float)Math.Cos(Orientation));
            Image = States.GameplayState.theTextureManager.GetTexture("BULLET");
            IsActive = true;
            CollisionRect = new Rectangle((int)Position.X, (int)Position.Y, Image.Width, Image.Height);
            ObjectType = Types.Bullet;
        }


        public override void Update(float ElapsedTime)
        {
            Position += Direction * 10;
            DestroyIfOutsideOfScreen();

            CollisionRect.X = (int)Position.X;
            CollisionRect.Y = (int)Position.Y;

        }
        public override void Render(float ElapsedTime, SpriteBatch theSpriteBatch)
        {
            theSpriteBatch.Draw(Image, Position, null, RenderColor, Orientation, new Vector2(Image.Width * 0.5f, Image.Height * 3.5f), 1.0f, SpriteEffects.None, 0);
        }

        public override void CheckCollision(BaseObject theObject)
        {
            
        }

        //This function destroys the object of the bullet if it goes outside of the screen.
        public void DestroyIfOutsideOfScreen()
        {
            if (Position.X >= 1920 + Image.Width || Position.X < 0 + Image.Width)
            {
                IsActive = false;
            }
            if (Position.Y >= 1080 + Image.Height || Position.Y < 0 + Image.Height)
            {
                IsActive = false;
            }
        }
    }
}